// let BASE_URL = "https://dev.mingqianyun.com/";
let BASE_URL = "https://www.mingqianyun.com/";//正式
const app = getApp();
const limit = 10000;

module.exports = {
  oauthLogin(event, cb) {
    if (event.errMsg == "getUserInfo:fail auth deny") {
    } else {
      wx.showLoading({
        title: "加载中"
      });
      wx.login({
        success(authRes) {
          if (authRes.code) {
            let userInfo = event.detail.userInfo;
            userInfo.iv = event.detail.iv;
            userInfo.encryptedData = event.detail.encryptedData;
            setTimeout(() => {
              wx.request({
                url: BASE_URL + "api/user/oauth_login",
                method: "POST",
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                data: {
                  code: authRes.code,
                  userinfo: JSON.stringify(userInfo),
                  type: "wxa_c"
                },
                success(_res) {
                  if (_res.data.error == 0) {
                    console.log("after oauth_login token=",_res.data.data.token);
                    wx.setStorageSync("user_info", _res.data.data);
                    wx.setStorageSync("token", _res.data.data.token);
                    app.globalData.userInfo = _res.data.data;
                    app.globalData.token = _res.data.data.token;
                    cb(_res.data);
                  }
                },
                complete(_res) {
                  console.log("complete", _res);
                  wx.hideLoading();
                }
              });
            }, 200);
          } else {
            console.log("登录失败！" + res.errMsg);
          }
        }
      });
    }
  },
  customerIndex: function(offset, limit, success) {
    wx.request({
      url: BASE_URL + "api/customer/index",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        offset: offset,
        limit: limit
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  fortuneteller: function(fid, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/customer/fortuneteller",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        fortuneteller_id: fid
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  follow: function(fid, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/follow/follow",
      method: "POST",
      header: {
        TOKEN: app.globalData.token,
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        fortuneteller_id: fid
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  unfollow: function(fid, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/follow/unfollow",
      method: "POST",
      header: {
        TOKEN: app.globalData.token,
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        fortuneteller_id: fid
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  reservedPeriods: function(date, service_id, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/order/periods",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        date: date,
        service_id: service_id
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  /**
   *
   * @param {日期时间 2020-03-06 08:00:00} datetime
   * @param {服务id} service_id
   */
  orderCreate: function(datetime, service_id, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/order/create",
      method: "POST",
      header: {
        TOKEN: app.globalData.token,
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        datetime: datetime,
        service_id: service_id
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  /**
   *
   * @param {订单id} id
   * @param {支付方式 1客户端小程序支付 2系统钱包支付} pay_type
   * @param {*} success
   */
  orderPay(id, pay_type, success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/order/pay",
      method: "POST",
      header: {
        TOKEN: app.globalData.token,
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        id: id,
        pay_type: pay_type
      },
      success(res) {
        // appId: "wx112389197e8513f6"
        // nonceStr: "5e8c25bf5e354"
        // package: "prepay_id=wx0715032734009849ef7ad1cd1012595400"
        // paySign: "1E796B1D2536FA63B32823775805D566"
        // signType: "MD5"
        // timeStamp: "1586243007"
       
      },
      complete(res) {
        if(res.statusCode == 200){
          if(pay_type == 1){
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: 'MD5',
              paySign: res.data.data.paySign,
              success (_res) {
                success(_res);
              },
              fail (_res) {
                console.log('fail',_res);
              }
            })
          } else {
            success({});
          }
        } else {
          if(res.data.msg == '钱包余额不足' && pay_type == 2){
            success(res.data);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
         
        }
        wx.hideLoading();
      }
    });
  },
  customerOrders(success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/customer/orders",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        offset: 0,
        limit: limit
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  customerMy(success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/customer/my",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        offset: 0,
        limit: limit
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  configs(success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/customer/configs",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  /**
   * 交易记录
   * @param {*} success
   */
  walletLogs(success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/customer/wallet_logs",
      method: "GET",
      header: {
        TOKEN: app.globalData.token
      },
      data: {
        offset: 0,
        limit: limit
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
  /**
   * 订单咨询记录
   * @param {*} oid 订单id
   * @param {*} success
   */
  chatLogs: function(id,success) {
    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: BASE_URL + "api/order/chat_logs",
      method: "GET",
      header: {
        TOKEN: app.globalData.token,
        'wxa-type':'wxa_c'
      },
      data:{
        id:id,
        offset:0,
        limit:10
      },
      success(res) {
        success(res.data);
      },
      complete(res) {
        wx.hideLoading();
      }
    });
  },
};
